﻿//开始调用Ajax
/*
url:要访问的链接   参数类型:String
paramMap:参数列表  参数类型:Map
callBack:回调接口  参数类型:js对象
注:callBack对象类一定要有onSuccess(ResultBean rb)方法和onFail(ResultBean rb)方法
*/
var map = new Map();

function AjaxStart(url, paramet, callBack) {
    if (map.get(url) == null) {
        map.put(url, "true");
    }
    else { return; }
    userid = userid == null ? "" : userid;
    token = token == null ? "" : token;
    //显示等待框
    wait.show("加载");
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: encodeURI(paramet + "&user_id=" + userid + "&token=" + token),
        success: function (json) {
            if (json == null) {
                toast.show("服务器返回数据失败");
                return;
            }
            wait.dismiss();
            if (json.code == 7) {
                //异地cookies 删掉
                delCookie("token");
                delCookie("userid");
                delCookie("logintype");
                delCookie("parentid");
            }

            if (json.code == 5 || json.code == 7 || json.code == 1003) {
                window.location.href = "login.html";
            }

            callBack(json);
            map.remove(url);
        },
        error: function (rs) {
            map.remove(url);
            toast.show("网络超时!");
        }
    });
}


var ajax = new AjaxData();



function AjaxData() {

    this.getAjax = function (Url, paramet, callback) {

        AjaxStart(Url, paramet, callback);
    }

    //注册
    this.Regist = function (paramet, callback) {

        AjaxStart(ajaxUrl.Regist, paramet, callback);
    }

    //登录
    this.Login = function (paramet, callback) {

        AjaxStart(ajaxUrl.Login, paramet, callback);
    }


    //获取详细信息
    this.GetDetail = function (paramet, callback) {

        AjaxStart(ajaxUrl.GetDetail, paramet, callback);
    }

    //修改密码
    this.Editpwd = function (paramet, callback) {

        AjaxStart(ajaxUrl.Editpwd, paramet, callback);
    }
    //忘记密码
    this.ForgetPwd = function (paramet, callback) {

        AjaxStart(ajaxUrl.ForgetPwd, paramet, callback);
    }


    //修改昵称
    this.Updatenickname = function (paramet, callback) {

        AjaxStart(ajaxUrl.Updatenickname, paramet, callback);
    }
    //修改个人信息
    this.UpdateInfo = function (paramet, callback) {

        AjaxStart(ajaxUrl.UpdateInfo, paramet, callback);
    }

    //获取文章默认封面
    this.GetArticleLogo = function (paramet, callback) {
        AjaxStart(ajaxUrl.GetArticleLogo, paramet, callback);
    }
    //文章列表
    this.getarticleList = function (paramet, callback) {

        AjaxStart(ajaxUrl.articleList, paramet, callback);
    }
    //文章添加
    this.Addarticle = function (paramet, callback) {

        AjaxStart(ajaxUrl.Addarticle, paramet, callback);
    }

    //文章详情
    this.ArticleDetail = function (paramet, callback) {

        AjaxStart(ajaxUrl.ArticleDetail, paramet, callback);
    }


    //文章修改
    this.ArticleUpdate = function (paramet, callback) {

        AjaxStart(ajaxUrl.ArticleUpdate, paramet, callback);
    }

    //文章浏览数
    this.ArticleClick = function (paramet, callback) {

        AjaxStart(ajaxUrl.ArticleClick, paramet, callback);
    }

    //文章移除
    this.Detelearticle = function (paramet, callback) {

        AjaxStart(ajaxUrl.Detelearticle, paramet, callback);
    }

    //默认频道
    this.defualtlist = function (paramet, callback) {
        AjaxStart(ajaxUrl.Getdefualtlist, paramet, callback);
    }
    //频道列表
    this.Getchelist = function (paramet, callback) {
        AjaxStart(ajaxUrl.Getlist, paramet, callback);
    }

    //添加收藏
    this.Addcollection = function (paramet, callback) {

        AjaxStart(ajaxUrl.Addcollection, paramet, callback);
    }
    //移除收藏
    this.Removecollection = function (paramet, callback) {

        AjaxStart(ajaxUrl.Removecollection, paramet, callback);
    }
    //收藏列表
    this.Getconlltionlist = function (paramet, callback) {

        AjaxStart(ajaxUrl.Getconlltionlist, paramet, callback);
    }

    //添加广告
    this.Addadvert = function (paramet, callback) {

        AjaxStart(ajaxUrl.Addadvert, paramet, callback);
    }
    //移除广告
    this.Removeadvert = function (paramet, callback) {

        AjaxStart(ajaxUrl.Removeadvert, paramet, callback);
    }
    //广告列表
    this.Getadvertlist = function (paramet, callback) {

        AjaxStart(ajaxUrl.Getadvertlist, paramet, callback);
    }
    //修改广告
    this.Editadvert = function (paramet, callback) {

        AjaxStart(ajaxUrl.Editadvert, paramet, callback);
    }
    //广告详细
    this.Detailadvert = function (paramet, callback) {

        AjaxStart(ajaxUrl.Detailadvert, paramet, callback);
    }
    //广告明细（文章详细页）
    this.DetailArticle = function (paramet, callback) {

        AjaxStart(ajaxUrl.DetailArticle, paramet, callback);
    }

    //添加分享
    this.AddShare = function (paramet, callback) {

        AjaxStart(ajaxUrl.AddShare, paramet, callback);
    }
    //移除分享
    this.RemoveShare = function (paramet, callback) {

        AjaxStart(ajaxUrl.RemoveShare, paramet, callback);
    }
    //分享列表
    this.GetSharelist = function (paramet, callback) {

        AjaxStart(ajaxUrl.GetSharelist, paramet, callback);
    }

    //统计汇总
    this.GetCount = function (paramet, callback) {
        AjaxStart(ajaxUrl.GetCount, paramet, callback);
    }


    //分享查看数
    this.Sharequery = function (paramet, callback) {
        AjaxStart(ajaxUrl.Sharequery, paramet, callback);
    }
    //分享点击数
    this.ShareClick = function (paramet, callback) {
        AjaxStart(ajaxUrl.ShareClick, paramet, callback);
    }


    //广告查看数
    this.Advertquery = function (paramet, callback) {
        AjaxStart(ajaxUrl.Advertquery, paramet, callback);
    }

    //广告点击数
    this.AdvertClick = function (paramet, callback) {
        AjaxStart(ajaxUrl.AdvertClick, paramet, callback);
    }

}
var ajaxUrl = new Url();
function Url() {

    //注册
    this.Regist = "Ajax/User.ashx?Regist";
    //登录
    this.Login = "Ajax/User.ashx?Login";
    //修改昵称
    this.Updatenickname = "Ajax/User.ashx?UpdateNickName";
    //修改个人信息
    this.UpdateInfo = "Ajax/User.ashx?UpdateInfo";

    //修改密码
    this.Editpwd = "Ajax/User.ashx?ModifyPwd";
    //忘记密码
    this.ForgetPwd = "Ajax/User.ashx?ForgetPwd";

    //获取详细信息
    this.GetDetail = "Ajax/User.ashx?GetDetail";

    //获取文章默认封面图
    this.GetArticleLogo = "Ajax/User.ashx?GetArticleLogo";

    //文章添加
    this.Addarticle = "Ajax/Article.ashx?Add";

    //文章详情
    this.ArticleDetail = "Ajax/Article.ashx?GetDetail";

    //文章修改
    this.ArticleUpdate = "Ajax/Article.ashx?Update";

    //文章浏览数
    this.ArticleClick = "Ajax/Article.ashx?Click";

    //文章移除
    this.Detelearticle = "Ajax/Article.ashx?Remove";
    //文章列表
    this.articleList = "Ajax/Article.ashx?GetList";

    //获取默认频道
    this.Getdefualtlist = "Ajax/Channel.ashx?GetDefaultList";
    //获取文章频道列表
    this.Getlist = "Ajax/Channel.ashx?GetList";

    //收藏
    this.Addcollection = "Ajax/Collect.ashx?Add";
    this.Removecollection = "Ajax/Collect.ashx?Remove";
    this.Getconlltionlist = "Ajax/Collect.ashx?GetList";

    //广告
    this.Addadvert = "Ajax/advert.ashx?Add";
    this.Removeadvert = "Ajax/advert.ashx?Remove";
    this.Getadvertlist = "Ajax/advert.ashx?GetList";
    this.Editadvert = "Ajax/advert.ashx?Update";
    this.Detailadvert = "Ajax/advert.ashx?GetDetail";
    this.DetailArticle = "Ajax/advert.ashx?GetDetailArticle";

    //分享
    this.AddShare = "Ajax/Shared.ashx?Add";
    this.RemoveShare = "Ajax/Shared.ashx?Remove";
    this.GetSharelist = "Ajax/Shared.ashx?GetList";

    //统计数量
    this.GetCount = "Ajax/Statistics.ashx?GetCount";

    //分享查看数
    this.Sharequery = "Ajax/Statistics.ashx?GetViewSharedList";
    //分享点击数
    this.ShareClick = "Ajax/Statistics.ashx?GetClickSharedList";


    //广告查看数
    this.Advertquery = "Ajax/Statistics.ashx?GetViewAdvertList";
    //广告点击数
    this.AdvertClick = "Ajax/Statistics.ashx?GetClickAdvertList"

    //上传图片
    this.Upimg = "Ajax/Upload.ashx?Img";

}

function Map() {
    var struct = function (key, value) {
        this.key = key;
        this.value = value;
    };
    // 添加map键值对 
    var put = function (key, value) {
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key === key) {
                this.arr[i].value = value;
                return;
            }
        };
        this.arr[this.arr.length] = new struct(key, value);
    };
    // 根据key获取value 
    var get = function (key) {
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key === key) {
                return this.arr[i].value;
            }
        }
        return null;
    };
    // 根据key删除 
    var remove = function (key) {
        var v;
        for (var i = 0; i < this.arr.length; i++) {
            v = this.arr.pop();
            if (v.key === key) {
                continue;
            }
            this.arr.unshift(v);
        }
    };
    // 获取map键值对个数 
    var size = function () {
        return this.arr.length;
    };
    // 判断map是否为空 
    var isEmpty = function () {
        return this.arr.length <= 0;
    };
    this.arr = new Array();
    this.get = get;
    this.put = put;
    this.remove = remove;
    this.size = size;
    this.isEmpty = isEmpty;
}


//--------------------------->ajax请求另一种实现<----------------------------

// 异步提交表单
function ajaxSubmit(obj) {

    // 默认参数
    var defObj = {
        form: "form"
    };

    // 默认获取action作url
    var url = $(defObj.form).attr("action");
    defObj.url = url;
    obj = $.extend({}, defObj, obj);
    ajaxJson(obj);
}

// 异步请求数据
function ajaxJson(obj) {

    // 默认参数
    var defObj = {
        type: "post",
        dataType: "json",
        cache: false,
        success: success,
        error: error
    };

    // 合并参数
    obj = $.extend({}, defObj, obj);

    // 公共参数
    var comParam = {};

    // 获取登录信息
    var userid = getCookie("userid");
    var token = getCookie("token");

    if (userid && token) {
        comParam = {
            user_id: userid,
            token: token
        }
    }

    // 合并参数
    obj.data = $.extend({}, comParam, obj.data);

    // 调试用,正式发布时请注释
    // obj.url = "/MicroAd" + obj.url;

    wait.show();
    if (obj.form) {
        $(obj.form).ajaxSubmit(obj);
    } else {
        $.ajax(obj);
    }

    // 获取数据成功回调
    function success(data) {
        wait.dismiss();
        if (data.code != 0) {
            if (data.code == 5 || data.code == 7 || data.code == 1003) {

                // 没有登录或者鞥路异常
                //异地cookies 删掉
                delCookie("token");
                delCookie("userid");
                delCookie("logintype");
                delCookie("parentid");

                window.location.href = "login.html";
            } else {
                toast.show(data.msg);
            }

            return;
        }
        if (obj.callback) {
            obj.callback(data.item);
        }
    }

    // 出错时回调
    function error() {
        toast.show("网络故障");
        wait.dismiss();
    }
}